package com.wxx.util;

import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.downloader.Downloader;
import us.codecraft.webmagic.processor.PageProcessor;

public class CrawlerUtil {
    public static Spider genSpider(PageProcessor pageProcessor, String startUrl, Downloader downloader) {
        Spider spider = Spider.create(pageProcessor);
        if (downloader != null) {
            spider.setDownloader(downloader);
        }
        if (startUrl != null && !startUrl.isEmpty()) {
            spider.addUrl(startUrl);
        }
        return spider;
    }

    public static Spider genSpider(PageProcessor pageProcessor, String startUrl) {
        return genSpider(pageProcessor, startUrl, null);
    }

    public static Spider genSpider(PageProcessor pageProcessor) {
        return genSpider(pageProcessor, null, null);
    }

    public static Spider genSpider(PageProcessor pageProcessor, Downloader downloader) {
        return genSpider(pageProcessor, null, downloader);
    }

    public static void configChromeDriver() {
        String driverPath = "";
        if (System.getProperties().getProperty("os.name").contains("Mac")) {
            driverPath = System.getProperty("user.dir") + "/src/main/resources/chromedriver";
        } else {
            driverPath = System.getProperty("user.dir") + "/src/main/resources/chromedriver.exe";
        }
        System.getProperties().setProperty("webdriver.chrome.driver", driverPath);
    }
}
